/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.service;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.cmcc.zysoft.infocollection.dao.MenuPcDao;
import com.cmcc.zysoft.infocollection.dao.RoleMenuPcDao;
import com.cmcc.zysoft.infocollection.model.Menu;
import com.cmcc.zysoft.infocollection.model.RoleMenu;
import com.starit.common.dao.hibernate.HibernateBaseDao;
import com.starit.common.dao.service.BaseServiceImpl;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: RoleMenuPcService.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-9-6 上午9:36:03
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Service
public class RoleMenuPcService extends BaseServiceImpl<RoleMenu, String> {
	
	@Resource
	private MenuPcDao menuPcDao;

	@Resource
	private RoleMenuPcDao roleMenuPcDao;
	
	@Override
	public HibernateBaseDao<RoleMenu, String> getHibernateBaseDao() {
		return this.roleMenuPcDao;
	}
	
	/**
	 * 通过角色ID获取角色对应的菜单.
	 * @param roleId 角色ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> getMenuByRoleId(String roleId) {
		return this.roleMenuPcDao.getMenuByRoleId(roleId);
	}
	
	/**
	 * 增加叶子节点菜单.
	 * @param roleId 角色ID
	 * @param menuId 菜单ID
	 * @return String
	 */
	public String add(String roleId, String menuId) {
		List<RoleMenu> list = this.roleMenuPcDao.findByNamedParam(new String[]{"roleId", "menuId"}, new Object[]{roleId, menuId});
		if(null != list && !list.isEmpty()) { //角色对应的菜单存在,不作任何操作
			return "";
		} else { //角色对应的菜单不存在,为其新增权限
			RoleMenu roleMenu = new RoleMenu();
			roleMenu.setRoleId(roleId);
			roleMenu.setMenuId(menuId);
			//添加父菜单
			this.addParentMenu(roleId, menuId);
			//添加子菜单
			return this.roleMenuPcDao.save(roleMenu);
		}
	}
	
	/**
	 * 通过指定菜单ID获得上级菜单-如果存在上级菜单,则为指定角色插入菜单的时候一并插入该菜单的上级菜单.
	 * @param roleId 角色ID
	 * @param menuId 菜单ID
	 */
	public void addParentMenu(String roleId, String menuId) {
		Menu menu = this.menuPcDao.get(menuId);
		if(!"0".equals(menu.getParentId())) {
			Menu parentMenu = this.menuPcDao.get(menu.getParentId());
			this.add(roleId, parentMenu.getMenuId());
		}
	}
	
	/**
	 * 删除叶子节点菜单.
	 * @param roleId 角色ID
	 * @param menuId 菜单ID
	 */
	public void delete(String roleId, String menuId) {
		List<RoleMenu> list = this.roleMenuPcDao.findByNamedParam(new String[]{"roleId", "menuId"}, new Object[]{roleId, menuId});
		if(null != list && !list.isEmpty()) {
			//删除子菜单
			this.roleMenuPcDao.delete(list.get(0));
			//删除父菜单
			this.deleteParentMenu(roleId, menuId);
		}
	}
	
	/**
	 * 通过指定菜单ID获得上级菜单-如果存在上级菜单,且删除该菜单后角色对应的菜单中无该上级菜单的子菜单,则删除父菜单.
	 * @param roleId 角色ID
	 * @param menuId 菜单ID
	 */
	public void deleteParentMenu(String roleId, String menuId) {
		Menu menu = this.menuPcDao.get(menuId);
		if(!"0".equals(menu.getParentId())) {
			Menu parentMenu = this.menuPcDao.get(menu.getParentId());
			int count = this.roleMenuPcDao.getLeafCount(roleId, parentMenu.getMenuId());
			if(count == 1) {
				this.delete(roleId, parentMenu.getMenuId());
			}
		}
	}
	
	/**
	 * 增加非叶子节点菜单,并递归添加其子菜单.
	 * @param roleId 角色ID
	 * @param menuId 菜单ID
	 */
	public void addAll(String roleId, String menuId) {
		//添加父菜单
		this.add(roleId, menuId);
		//根据父菜单ID获取其子菜单
		List<Map<String, Object>> subMenuList = this.menuPcDao.getSubMenu(menuId);
		if(null != subMenuList && !subMenuList.isEmpty()) {
			for(Map<String, Object> map : subMenuList) {
				if("Y".equals(map.get("isLeaf").toString())) {
					this.add(roleId, map.get("menuId").toString());
				} else {
					this.addAll(roleId, map.get("menuId").toString());
				}
			}
		}
	}
	
	/**
	 * 删除非叶子节点菜单,并递归删除其子菜单.
	 * @param roleId 角色ID
	 * @param menuId 菜单ID
	 */
	public void deleteAll(String roleId, String menuId) {
		//删除父菜单
		this.delete(roleId, menuId);
		//根据父菜单ID获取其子菜单
		List<Map<String, Object>> subMenuList = this.menuPcDao.getSubMenu(menuId);
		if(null != subMenuList && !subMenuList.isEmpty()) {
			for(Map<String, Object> map : subMenuList) {
				if("Y".equals(map.get("isLeaf").toString())) {
					this.delete(roleId, map.get("menuId").toString());
				} else {
					this.deleteAll(roleId, map.get("menuId").toString());
				}
			}
		}
	}
}